#!/bin/bash

echo "🔨 简化构建 - 避免网络依赖"
echo "================================"

# 清理并重新构建
rm -rf bin .objs
mkdir -p bin .objs

# 编译设置 - 添加 NO_CURL 定义
INCLUDES="-I../cbp2make/lib -I../cbp2make/lib/tinyxml -I../cbp2make/src -Ilib -Isrc"
CXXFLAGS="-std=c++11 -DAICODER_ENHANCED -m64 -DNO_CURL"

echo "📝 编译核心文件..."

# 编译函数
compile_file() {
    local file=\$1
    local base=\$(basename "\$file" .cpp)
    g++ \$INCLUDES \$CXXFLAGS -c "\$file" -o ".objs/\$base.o" >/dev/null 2>&1
    if [ \$? -eq 0 ]; then
        echo "  ✅ \$base.cpp"
        return 0
    else
        echo "  ❌ \$base.cpp"
        return 1
    fi
}

# 编译 AI 模型文件
echo "🔸 AI 模型文件:"
compile_file "src/ai_model_config.cpp"
compile_file "src/unified_ai_client.cpp"
compile_file "src/aicoder_enhancements.cpp"

# 编译 cbp2make 核心文件
echo ""
echo "🔸 cbp2make 核心文件:"
for file in ../cbp2make/src/*.cpp; do
    base=\$(basename "\$file" .cpp)
    if [ ! -f ".objs/\$base.o" ]; then
        compile_file "\$file"
    fi
done

# 编译 STL 工具库（确保 stringhash.cpp 被编译）
echo ""
echo "🔸 STL 工具库:"
for file in ../cbp2make/lib/stl*.cpp; do
    base=\$(basename "\$file" .cpp)
    if [ ! -f ".objs/\$base.o" ]; then
        compile_file "\$file"
    fi
done

# 编译 TinyXML 核心库
echo ""
echo "🔸 TinyXML 核心库:"
for file in ../cbp2make/lib/tinyxml/*.cpp; do
    base=\$(basename "\$file" .cpp)
    if [ "\$base" != "xmltest" ] && [ ! -f ".objs/\$base.o" ]; then
        compile_file "\$file"
    fi
done

# 链接（不链接 curl）
echo ""
echo "🔗 链接可执行文件..."
g++ -m64 .objs/*.o -o bin/cbp2make_ai

if [ -f "bin/cbp2make_ai" ]; then
    echo ""
    echo "🎉 简化构建成功!"
    echo "📊 统计:"
    echo "   - 对象文件: \$(find .objs -name "*.o" | wc -l) 个"
    echo "   - 可执行文件: bin/cbp2make_ai"
    echo ""
    echo "💡 注意: 当前为模拟模式，需要安装 curl 才能使用真实 AI 功能"
else
    echo "❌ 简化构建失败"
    exit 1
fi
